﻿using _07_04_DBHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _07_DBHelper的使用
{
    public partial class FrmEdit : Form
    {
        public string StuId;
        public FrmEdit()
        {
            InitializeComponent();
        }

        public FrmEdit(string StuId)
        {
            //MessageBox.Show(StuId);
            this.StuId = StuId;
            InitializeComponent();
            this.textBox1.Text = StuId;
        }
        private void BindDetail()
        {
            string sql = "select * from StudentInfo where StuID=@StuID";
            DBHelper.PrepareCmd(sql);
            DBHelper.SetParamter("StuID", this.StuId);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecQuery();
            if (dt.Rows.Count != 1)
            {
                MessageBox.Show("未找到该学生信息");
                this.Close();
                return;
            }
            this.textBox2.Text = dt.Rows[0]["StuName"].ToString();
            this.textBox3.Text = dt.Rows[0]["StuAge"].ToString();
            if (dt.Rows[0]["StuSex"].ToString().Equals("男"))
            {
                this.checkBox1.Checked = true;
            }
            else
            {
                this.checkBox2.Checked = true;
            }
            string[] arrHobby = dt.Rows[0]["StuHobby"].ToString().Split(',');
            foreach (string hobby in arrHobby)
            {
                foreach (CheckBox item in this.panel2.Controls)
                {
                    if (item.Text.Equals(hobby)) { 
                    item.Checked = true;
                    }
                }
            }
            this.cmbName.SelectedValue = dt.Rows[0]["ProfessionID"].ToString();
        }
        private void BindPro()
        {
            DataTable dt = new DataTable();
            DBHelper.PrepareCmd("select * from ProfessionInfo");
            dt = DBHelper.ExecQuery();
            //DataRow dr = dt.NewRow();
            //dr["professionID"] = "0";
            //dr["professionName"] = "请选择";
            //dt.Rows.InsertAt(dr, 0);
            this.cmbName.DataSource = dt;
            this.cmbName.DisplayMember = "professionName";
            this.cmbName.ValueMember = "professionID";
        }

        private void FrmEdit_Load(object sender, EventArgs e)
        {
            BindPro();
            BindDetail();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql= "update StudentInfo set StuName=@StuName,StuAge=@StuAge,StuSex=@StuSex,StuHobby=@StuHobby,ProfessionID=@ProfessionID where StuID=@StuID";
            DBHelper.PrepareCmd(sql);
            DBHelper.SetParamter("StuID", this.textBox1.Text);
            DBHelper.SetParamter("StuName", this.textBox2.Text);
            DBHelper.SetParamter("StuAge", this.textBox3.Text);
            string sex = "";
            if (this.checkBox1.Checked == true)
            {
                sex = this.checkBox1.Text;
            }
            if (this.checkBox2.Checked == true)
            {
                sex = this.checkBox2.Text;
            }
            DBHelper.SetParamter("StuSex", sex);
            string hobby = "";
            foreach (CheckBox item in this.panel2.Controls)
            {
                if (item.Checked)
                {
                    if (!hobby.Equals(""))
                    {
                        hobby += ",";
                    }
                    hobby += item.Text;
                }
            }
            DBHelper.SetParamter("StuHobby", hobby);
            DBHelper.SetParamter("ProfessionID", this.cmbName.SelectedValue.ToString());
            int count = DBHelper.ExecNonQuery();
            if (count == 1)
            {
                MessageBox.Show("修改成功");
                (this.Owner as Form1).BindData();
                this.Close();
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }
    }
}
